#!/bin/bash

SUBJECTS_DIR="/media/amd/data/long/mdd_3d_constr"
SAV_DIR="/media/amd/data/long/parc_tables/mdd_3d_constr_dk68"
MAX_JOBS=50
CSV_FILE="pat_effi_recon_all.csv"

process_subject() {
    local subj=$1
    local lh_path="$SAV_DIR/${subj}.scheafer400.stats.lh.table"
    local rh_path="$SAV_DIR/${subj}.scheafer400.stats.rh.table"

     mri_surf2surf --srcsubject fsaverage \
        --sval-annot /media/amd/data/long/lh.Schaefer2018_400Parcels_17Networks_order.annot \
        --trgsubject $subj \
        --trgsurfval $SUBJECTS_DIR/$subj/label/lh.scheafer400.annot 
        --hemi lh

    mri_surf2surf --srcsubject fsaverage \
        --sval-annot /media/amd/data/long/rh.Schaefer2018_400Parcels_17Networks_order.annot \
        --trgsubject $subj \
        --trgsurfval $SUBJECTS_DIR/$subj/label/rh.scheafer400.annot \
        --hemi rh

    mris_anatomical_stats -a "$SUBJECTS_DIR/$subj/label/lh.scheafer400.annot" \
                             -b -f "$lh_path" "$subj" lh

    mris_anatomical_stats -a "$SUBJECTS_DIR/$subj/label/rh.scheafer400.annot" \
                             -b -f "$rh_path" "$subj" rh
}

export -f process_subject
export SUBJECTS_DIR
export SAV_DIR

# 读取CSV文件并获取第一列的所有sub（跳过表头）
tail -n +2 "$CSV_FILE" | cut -d ',' -f 1 | parallel -j $MAX_JOBS --progress --joblog parallel_joblog.txt \
    "process_subject {}"

echo "All tasks have been completed - see parallel_joblog.txt"
